package com.tcy.bigevent.controller;

import com.tcy.bigevent.pojo.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploadController {
    @Value("${file.upload-dir}")
    private String uploadDir;
    //把文件内容存储到本地磁盘上
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        String filePath = uploadDir + fileName;
        file.transferTo(new File(filePath));
        return Result.success(fileName);
    }
}
